package com.seveneight.baijiesteward.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.mob.MobSDK;
import com.seveneight.baijiesteward.utils.AppContextUtil;
import com.seveneight.baijiesteward.utils.AppUtil;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by user1 on 2018/1/13.
 */

public class MyApplication extends Application {
    public static Context context;
    private static MyApplication instance;
    public static Set<Activity> activitys = new HashSet<Activity>();


    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        context = getApplicationContext();
        AppContextUtil.init(context);
        MobSDK.init(context);
        JPushInterface.setDebugMode(true);
        JPushInterface.init(instance);
        initCrash();
    }

    //异常捕获
    private void initCrash() {
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = AppUtil.getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(context, "e90aa04830", true, strategy);
    }

    //返回
    public static Context getContext() {
        return context;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    public void addActivity(Activity activity) {
        activitys.add(activity);
    }

    public void removeActivity(Activity activity) {
        activitys.remove(activity);
    }

    public void finishAllActivity() {
        for (Activity activity : activitys) {
            activity.finish();
        }
    }

    //完全退出APP，关闭进程
    public void exit() {
        finishAllActivity();
        android.app.ActivityManager am = (android.app.ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

}
